package test.core.data;

import org.junit.Test;

import hyl.core.MyDate;
import hyl.core.MyFun;
import hyl.core.data.ExBlockMap;
import hyl.core.data.BlockQueue;
import hyl.core.data.BlockValue;


public class test {
	// @Test
	public void test() {
		ExBlockMap<String, String> bm = new ExBlockMap<String, String>(10);

		Thread t1 = new Thread(() -> {
			System.out.println("t1：");
			MyDate.sleep(1000);
			String bString = bm.get("b1");
			System.out.println("t1=" + bString);
		});
		Thread t2 = new Thread(() -> {
			System.out.println("t2：");
			MyDate.sleep(4000);
			bm.put("b1", "dd");
			System.out.println("t2.3：写入");
		});
		Thread t3 = new Thread(() -> {
			System.out.println("t3：");
			MyDate.sleep(2000);
			String bString = bm.get("b3");
			System.out.println("t3=" + bString);
		});
		Thread t4 = new Thread(() -> {
			System.out.println("t4：");		
			MyDate.sleep(5000);
			bm.put("b3", "王企鹅");
			System.out.println("t4.3：写入");
		});
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		MyDate.sleep(30000);
	}

	// @Test
	public void test2() {
		BlockValue<String> bm = new BlockValue<String>(1000);

		Thread t2 = new Thread(() -> {
			long st = MyFun.getMs();
			System.out.println("t2：");
			MyDate.sleep(3000);
			bm.set("dd");
			long len = MyFun.getMs() - st;
			System.out.println("t2：" + len + "毫秒后写入");
		});
		Thread t3 = new Thread(() -> {
			long st = MyFun.getMs();
			System.out.println("t3：");
			String ppString = bm.get();
			System.out.println("t3-1：" + ":" + ppString);
			long len = MyFun.getMs() - st;
			System.out.println("t3-1：1秒后读取");
			ppString = bm.get();
			System.out.println("t3-2：" + ":" + ppString);
			len = MyFun.getMs() - st;
			System.out.println("t3-2：2秒后读取");
			ppString = bm.get();
			System.out.println("t3-3：" + ":" + ppString);
			len = MyFun.getMs() - st;
			System.out.println("t3-3：3秒后读取");
			System.out.println("t3-4：" + ":" + bm.get());

		});

		t2.start();
		t3.start();

		MyDate.sleep(30000);
	}

	// @Test
	public void test3() {
		BlockValue<String> bm = new BlockValue<String>(1000);

		Thread t2 = new Thread(() -> {
			long st = MyFun.getMs();
			MyDate.sleep(2000);
			long len = MyFun.getMs() - st;
			System.out.println("t2：" + len + "毫秒后写入");
			bm.set("dd");
		});

		Thread t4 = new Thread(() -> {
			long st = MyFun.getMs();
			System.out.println("t4：" + ":" + bm.get());
			long len = MyFun.getMs() - st;
			System.out.println("t4：" + len);

		});
		t2.start();

		t4.start();
		MyDate.sleep(30000);
	}

	// @Test
	public void test3_2() {
		BlockValue<String> bm = new BlockValue<String>(0);

		Thread t2 = new Thread(() -> {
			long st = MyFun.getMs();
			System.out.println("t2：");
			MyDate.sleep(3000);
			long len = MyFun.getMs() - st;
			System.out.println("t2：" + len + "毫秒后写入");
			bm.set("dd");
			bm.set("dd1");
			bm.set("dd2");
			bm.set("dd3");
			bm.set("dd4");
		});

		Thread t3 = new Thread(() -> {
			long st = MyFun.getMs();
			System.out.println("t3：");
			MyDate.sleep(2500);
			System.out.println("t3：" + ":" + bm.get());
			long len = MyFun.getMs() - st;
			System.out.println("t3：" + len);

		});
		Thread t4 = new Thread(() -> {
			long st = MyFun.getMs();
			System.out.println("t4：");
			MyDate.sleep(2500);
			System.out.println("t4：" + ":" + bm.get());
			long len = MyFun.getMs() - st;
			System.out.println("t4：" + len);

		});
		t2.start();
		t3.start();
		t4.start();
		MyDate.sleep(30000);
	}

	// @Test
	public void test3_3() {
		BlockQueue<String> bm = new BlockQueue<String>(1000);

		Thread t2 = new Thread(() -> {
			long st = MyFun.getMs();
			String string = null;
			for (int i = 0; i < 1000000; i++) {
				string = "dd" + i;
				bm.set(string);
			}
			long len = MyFun.getMs() - st;
			System.out.println("t2：" + string + "-" + len + "毫秒后写入ok");
			MyDate.sleep(1000);
			bm.set("ccc");
			len = MyFun.getMs() - st;
			System.out.println("t2：" + len + "毫秒后写入ok");
		});
		Thread t3 = new Thread(() -> {
			long st = MyFun.getMs();
			MyDate.sleep(5);
			int k = 500000 - 2;
			for (int i = 0; i < k; i++) {
				String tString = bm.get();
				if (tString == null)
					System.out.println("空4=" + i);
			}
			long len = MyFun.getMs() - st;
			System.out.println("t3：" + len + "毫秒后读取ok");
			for (int i = 0; i < 2; i++) {
				System.out.println("t3：最后读取=" + bm.get());
			}
		});
		Thread t4 = new Thread(() -> {
			long st = MyFun.getMs();
			MyDate.sleep(5);
			int k = 500000 - 3;
			for (int i = 0; i < k; i++) {
				String tString = bm.get();
				if (tString == null)
					System.out.println("空4=" + i);
			}
			long len = MyFun.getMs() - st;
			System.out.println("t4：" + len + "毫秒后读取ok");
			for (int i = 0; i < 5; i++) {
				System.out.println("t4：最后读取=" + bm.get());
			}
//		
		});
		t2.start();
		t3.start();
		t4.start();
		try {
			t2.join();
			t3.join();
			// t4.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void test3_4() {
		BlockValue<String> bm = new BlockValue<String>(3000);

		Thread t2 = new Thread(() -> {
			long st = MyFun.getMs();
			for (int i = 0; i < 2000; i++) {
				// System.out.println(i);
				MyDate.sleep(1);
				bm.set("dd" + i);
			}
			long len = MyFun.getMs() - st;
			System.out.println("t2：" + len + "毫秒后写入ok");

		});
		Thread t3 = new Thread(() -> {
			long st = MyFun.getMs();
			//MyDate.sleep(5);
			for (int i = 0; i < 2000; i++) {
				MyDate.sleep(1);
				System.out.println("t3：最后读取=" + bm.get());
			}
			long len = MyFun.getMs() - st;
			System.out.println("t3：" + len + "毫秒后读ok");
		});

		t2.start();
		t3.start();
		try {
			t2.join();
			t3.join();

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//@Test
	public void test4_1() {
		// 要求get之后才能set
		// 第二次get 会自动清除掉之前的set
		BlockValue<String> bm = new BlockValue<String>(2000);
		Thread t2 = new Thread(() -> {
			// System.out.println("t2：" + bm.get());
			System.out.println("t2：最后读取=" + bm.get());
			MyDate.sleep(500);
			// bm.clear();
			System.out.println("t2：最后读取=" + bm.get());

		});
		t2.start();

		// bm.set("dd");
		Thread t3 = new Thread(() -> {
			MyDate.sleep(2100);
			bm.set("dd");
			MyDate.sleep(1000);
			bm.set("ddc");

		});
		t3.start();
		try {
			t2.join();
			t3.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
